package com.tiamo.traders.service.serviceImpl;

import com.tiamo.traders.entity.User;
import com.tiamo.traders.mapper.UserMapper;
import com.tiamo.traders.service.UserSeervice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserSeervice {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public String userLogin(User user) {
        String  result="";
//        redisTemplate;
        if (!user.getSessionCode().equals(user.getCode())) {
            result="error-code";
            return result;
        }
        User user1 = userMapper.userLogin(user);
        if (user1 == null) {
            result="error";
            return result;
        }else {
            String token=user.getName();
            redisTemplate.boundValueOps(token).set(user,60, TimeUnit.MINUTES);
            result="success";
        }
        return result;
    }

    @Override
    public User userFindById(Integer id) {
        User user1 = userMapper.userFindById(id);
        return user1;
    }

    @Override
    public Integer userUpdate(User user) {
        Integer res= userMapper.userUpdate(user);
        return res;
    }

    @Override
    public Integer userDeleteById(Integer id) {
        int res=userMapper.userDeleteById(id);
        return res;
    }

    @Override
    public Integer userGetCount(User user) {
        Integer count=userMapper.userGetCount(user);
        return count;
    }

    @Override
    public List<User> userFindAll(User user) {
        List<User> userList=userMapper.userFindAll(user);
        return userList;
    }

    @Override
    public Integer userUpdateEnable(User user) {
        return userMapper.userUpdateEnable(user);
    }

    @Override
    public Integer userAdd(User user) {

        return userMapper.userAdd(user);
    }
}
